package com.fanhao.dllo.justforyou.activity;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.AfficheViewPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class GoodByeActivtiy extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private AfficheViewPagerAdapter pagerAdapter;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    //图片的数组
    private int []image = {R.mipmap.goodbye1, R.mipmap.goodbye2, R.mipmap.goodbye3,
            R.mipmap.goodbye4, R.mipmap.goodbye5, R.mipmap.goodbye6, R.mipmap.goodbye7};

    private ImageView[] image_small = new ImageView[7];

//延时跳转对象
    private Handler handler;

    private LayoutInflater inflater;
    private int maxItem = 0;

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            int index = viewPager.getCurrentItem();
            viewPager.setCurrentItem(++index);
            //延时两秒改变下标
            handler.postDelayed(r, 3000);
        }
    };
    private LinearLayout v;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_bye_activtiy);

        viewPager = (ViewPager)findViewById(R.id.viewPager);
        pagerAdapter = new AfficheViewPagerAdapter();
        viewPager.setAdapter(pagerAdapter);

        //遍历数组
        for (int i = 0; i < image.length; i ++){
            Map<String, Object> map = new HashMap<>();
            map.put("image", image[i]);

            list.add(map);
        }
        maxItem = list.size();//设置最大长度
        handler = new Handler();//初始化适配器
        handler.postDelayed(r, 3000);//延时改变下标

        pagerAdapter.addData(list);//添加到适配器


        v = (LinearLayout)findViewById(R.id.indicator);

        viewPager.addOnPageChangeListener(this);

        initIndicator();

    }

    private void initIndicator() {
        ImageView imageView;
        //线性水平布局 负责动态调整导航图标

        //创建小圆点
        for (int i = 0; i < image.length; i ++ ) {
            imageView = new ImageView(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
            layoutParams.setMargins(20, 30, 20, 30);
            imageView.setLayoutParams(layoutParams);
            image_small [i] = imageView;

            if (i == 0){//初始化 第一个为选中状态
                image_small[i].setBackgroundResource(R.mipmap.iconfont_yuandian_2);
            }else{
                image_small[i].setBackgroundResource(R.mipmap.iconfont_yuandian_3);
            }
            v.addView(image_small[i]);//将小圆点数组 添加到线性布局中
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //遍历数组 设置小圆点的图片
        for (int i = 0; i < image_small.length; i ++){
            image_small[(position%list.size())].setBackgroundResource(R.mipmap.iconfont_yuandian_2);
            //判断 如果当前位置不等于i 则设置成另一个图片
            if ((position%list.size()) != i){
                image_small[i].setBackgroundResource(R.mipmap.iconfont_yuandian_3);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
